package NWish.utils
{
	import NWish.Interface.ILogger;
	import NWish.Misc;
	import NWish.json.Json;
	
	import com.adobe.mp3.MP3Decoder;
	import com.adobe.utils.StringUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.*;
	import flash.net.SharedObject;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class NWLoader
	{
		static private var _loadLocal:Boolean = false;
		
		static private var _version = 0;
		
		
		//URLLoaderDataFormat.BINARY
		//URLLoaderDataFormat.TEXT
		public function NWLoader(url,onComplete:Function,dataFormat)
		{
			if(loadLocal)
			{
				var data = load(url);
				if(data)
				{
					Misc.callLater(1,function(e){onComplete(data)});
					return;
				}
			}
			
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = dataFormat;
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, Misc.securityErrorHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, Misc.ioErrorHandler);
			
			
			var request:URLRequest = new URLRequest(url);
			loader.load(request);
			
			
			function completeHandler(e):void
			{
				var l:URLLoader = URLLoader(e.target);
				onComplete(l.data);
				
				
				//
				if(loadLocal)
					save(url,l,dataFormat);
			}
		}
		
		private function replace_url(url:String)
		{
			url = StringUtil.replace(url,'/','__');
			url = StringUtil.replace(url,'\\','__');
			return url;
		}
		private function load(url:String)
		{
			url = replace_url(url);
			var so:SharedObject = SharedObject.getLocal(url);
			
			if(so.size > 0)
			{
				if(so.data.format == URLLoaderDataFormat.BINARY)
				{
					return so.data.data;
				}
				else if(so.data.format == URLLoaderDataFormat.TEXT)
				{
					return so.data.data;
				}
				else
					throw new Error();
			}
		}
		
		
		private function save(url,l,dataFormat)
		{
			url = replace_url(url);
			var so:SharedObject = SharedObject.getLocal(url);
			
			if(dataFormat == URLLoaderDataFormat.BINARY)
			{
				so.data.data = l.data;
				so.data.format = URLLoaderDataFormat.BINARY;
			}
			else if(dataFormat == URLLoaderDataFormat.TEXT)
			{
				so.data.data = l.data;
				so.data.format = URLLoaderDataFormat.TEXT;
			}
			else
				throw new Error();
			
			so.flush();
			
			addNameList(url);
		}
		
		static private function clearLocal(v)
		{
			var so:SharedObject = SharedObject.getLocal('version');
			
			for each(var n in so.data.names)
			{
				var dso:SharedObject = SharedObject.getLocal(n);
				dso.clear();
			}
			
			so.data.names = [];
		}
		
		static private function addNameList(url)
		{
			var so:SharedObject = SharedObject.getLocal('version');
			
			if(so.data.names == null)
				so.data.names = [];
			
			so.data.names.push(url);
		}
		
		
		static public function get version()
		{
			if(_version)
				return _version;
			
			var so:SharedObject = SharedObject.getLocal('version');
			_version = so.data.version;
			return _version;
		}
		
		static public function set version(v)
		{
			var so:SharedObject = SharedObject.getLocal('version');
			
			if(so.data.version == v)
				return;
			else
			{
				clearLocal(so.data.version);
				so.data.version = v;
				so.flush(10000);
			}
		}
		
		static public function get loadLocal():Boolean
		{
			return _loadLocal;
		}
		
		static public function set loadLocal(b:Boolean)
		{
			_loadLocal = b;
			
			if(_loadLocal)
			{
				//todo...
				//弹窗口要求更多的本地存储空间
			}
		}
		
		
		//
		static public function bitmap(url,onComplete:Function,user_data = null)
		{
			new NWLoader(url, onLoadComplete, URLLoaderDataFormat.BINARY);
			
			function onLoadComplete(data)
			{
				var load:Loader = new Loader();
				load.loadBytes(data);
				load.contentLoaderInfo.addEventListener(Event.COMPLETE, onBitmapComplete);
			}
			
			function onBitmapComplete(e:Event)
			{
				e.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onBitmapComplete);
				
				if(user_data != null)
					onComplete(e.target.content,user_data);
				else
					onComplete(e.target.content);
			}
		}
		
		//
		static public function mp3(url,onComplete:Function,user_data = null)
		{
			new NWLoader(url, onLoadComplete, URLLoaderDataFormat.BINARY);
			
			function onLoadComplete(data)
			{
				var mp3d:MP3Decoder = new MP3Decoder();
				mp3d.decode(data,onMp3Complete);
			}
			
			function onMp3Complete(sndClass:Class)
			{
				if(user_data != null)
					onComplete(new sndClass(),user_data);
				else
					onComplete(new sndClass());
			}
		}
		
		static public function text(url,onComplete:Function, __loadLocal:Boolean)
		{
			var temp = NWLoader.loadLocal;
			if(__loadLocal == false)
				NWLoader.loadLocal == __loadLocal;

			new NWLoader(url, onComplete, URLLoaderDataFormat.TEXT);
			
			if(__loadLocal == false)
				NWLoader.loadLocal == temp;
		}

		static public function json(url,onComplete:Function, __loadLocal:Boolean)
		{
			var temp = NWLoader.loadLocal;
			if(__loadLocal == false)
				NWLoader.loadLocal == __loadLocal;

			new NWLoader(url, onLoadComplete, URLLoaderDataFormat.BINARY);
			
			if(__loadLocal == false)
				NWLoader.loadLocal == temp;
			
			//
			function onLoadComplete(data)
			{
				onComplete(Json.decode(data));
			}
		}
		
	}
}






